package com.wb.config

import com.wb.component.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.web.client.RestTemplateBuilder
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.client.ClientHttpRequestFactory
import org.springframework.http.converter.HttpMessageConverter
import org.springframework.http.converter.StringHttpMessageConverter
import org.springframework.web.client.RestTemplate
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets
import javax.annotation.Resource

@Slf4j
@Configuration
open class RestConfig {

    @Bean
    open fun restTemplate(): RestTemplate {

        val restTemplate: RestTemplate = RestTemplateBuilder().build()

//        restTemplate.getMessageConverters().set(1, StringHttpMessageConverter(StandardCharsets.UTF_8));

        val list = restTemplate.messageConverters
        for (converter in list) {
            if (converter is StringHttpMessageConverter) {
                (converter as StringHttpMessageConverter).defaultCharset = Charset.defaultCharset()
                break
            }
        }

        return restTemplate;

    }

}